package mytest.aop.config.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LogAspect {

    /**
     * @Aroud等注解，都会有类似AspectJAroundAdvice的Advice（before-》AspectJMethodBeforeAdvice、after-》AspectJAfterAdvice等），
     * 参考invoke方法，里面传的参数就是ProceedingJoinPoint
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("@annotation(mytest.aop.config.annotation.LogHandler)")
//    @Around(value = "execution(* mytest.aop.config.annotation.*.*(..))")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        Object object = joinPoint.getTarget();
        Class<?> clazz = object.getClass();
        System.out.println("目标对象：" + clazz.getName());
        System.out.println("方法：" + joinPoint.getSignature().getName());





        System.out.println("方法执行后");
        long start = System.currentTimeMillis();

        Object proceed = joinPoint.proceed();

        long executionTime = System.currentTimeMillis() - start;

        System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
        return proceed;
    }
}
